package com.stock.demo.framework.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.stock.demo.R;

/**
 * Created by XuZhaoyu on 2017/5/22.
 */

public class LoadingDialog extends Dialog {

    private View mView;
    private ImageView mIvLoading;
    private TextView mTvTitle;

    public LoadingDialog(Context context) {
        super(context, R.style.loading_dialog);
        initView();
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loading, null);
        mIvLoading = (ImageView) mView.findViewById(R.id.iv_loading);
        mTvTitle = (TextView) mView.findViewById(R.id.tv_message);
        initLoadingAnimation();
        setCancelable(false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
    }

    private void initLoadingAnimation() {
        mIvLoading.setImageResource(R.drawable.loading_icon);
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.loading_animation);
        mIvLoading.startAnimation(hyperspaceJumpAnimation);
    }

    public LoadingDialog setText(String text) {
        if (TextUtils.isEmpty(text)) {
            return this;
        }
        mTvTitle.setText(text);
        return this;
    }

    public void build() {
        if (TextUtils.isEmpty(mTvTitle.getText().toString().trim())) {
            mTvTitle.setVisibility(View.GONE);
        }
        show();
    }

}
